package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.RepulseAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.rxretrofit_build.util.MapUtil;

import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import butterknife.Bind;
import butterknife.OnClick;
import master_bean.ToDayBean;
import rxretrofit_base.model.KeyBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by yidong on 2017/8/14.
 */

public class RepulseHistoryActivity extends BaseActivity implements SpringView.OnFreshListener, MultiItemTypeAdapter.OnItemClickListener{
   @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.spring_view)
    SpringView springView;
    @Bind(R.id.iv_return)
    LinearLayout returnLin;
    @Bind(R.id.tv_title)
    TextView titleTv;
    private RepulseAdapter mReAdapter;
    private List<ToDayBean> data;
    @Override
    protected int getLayout() {
        return R.layout.activity_repulse_history;
    }

    @Override
    protected void initEventAndData() {
        returnLin.setVisibility(View.VISIBLE);
        titleTv.setText("打回历史");
        springView.setListener(this);
        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));
        springView.setType(SpringView.Type.FOLLOW);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        getData();
    }

    @Override
    protected void createPresenter() {

    }
    @OnClick(R.id.iv_return)
    public void onClick(){
        finish();
    }
    private void getData() {
        RxRequest.request(ShowType.DISMISS, RxRequest.getClient(this, new MapUtil().build()).create(IApiActivityRequest.class).repulseHistory(),
                new BaseSubscriber<TokenBean<List<ToDayBean>>>(this) {
                    @Override
                    public void onSuccess(TokenBean<List<ToDayBean>> o) {

                        if(o.getData()!=null && o.getData().size() >0){
                            refreshData(o.getData());
                        }else{
                            recyclerView.setAdapter(null);
                            showToast("没有打回数据！");
                        }

                    }

                    @Override
                    public void onFailure(Throwable e) {
                        springView.onFinishFreshAndLoad();
                    }

                    @Override
                    public void onStart() {
                       showLoading();
                    }

                    @Override
                    public void onCompleted() {
                        springView.onFinishFreshAndLoad();
                       cancelLoading();
                    }
                });
    }

    private void refreshData(List<ToDayBean> data) {
        this.data = data;
        if(mReAdapter == null){
            mReAdapter = new RepulseAdapter(this, R.layout.layout_item_repulsehistory, data);
            mReAdapter.setOnItemClickListener(this);
            recyclerView.setAdapter(mReAdapter);
        }else{
            mReAdapter.refresh(data);
            mReAdapter.notifyDataSetChanged();
        }
    }



    /**
     * @param view
     * @param holder
     * @param position
     */
    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        CustomSharedPreferences.saveString(this,new MapUtil().put(KeyBean.CARID.valueOf(),data.get(position).getCar_id()).build(), KeyBean.FILE_NAME.valueOf());
        startIntent(new Intent(RepulseHistoryActivity.this, BasicInfoActivity.class),false);
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        getData();
    }

    /**
     * 上拉加载
     */
    @Override
    public void onLoadmore() {
        getData();
    }
}
